package financial.sharing.center.payable.service.impl;

import com.github.pagehelper.PageInfo;
import financial.sharing.center.common.enums.AccruedOperationStageEnum;
import financial.sharing.center.common.enums.EnableStatusEnum;
import financial.sharing.center.common.enums.ResponseContext;
import financial.sharing.center.common.utils.BigDecimalUtil;
import financial.sharing.center.payable.dao.PayableAccruedMapper;
import financial.sharing.center.payable.model.entity.PayableAccrued;
import financial.sharing.center.payable.service.PayableAccruedService;
import financial.sharing.center.payable.service.PayableBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PayableAccruedServiceImpl
 * @Description TODO
 * @Author guoyuhang
 * @ @csdn https://blog.csdn.net/qq_40788718
 * @Date 2021/5/19
 * @Version 1.0
 */
@Service
public class PayableAccruedServiceImpl implements PayableAccruedService {

    @Autowired
    private PayableAccruedMapper payableAccruedMapper;

    @Autowired
    private PayableBillService payableBillServiceImpl;

    @Override
    public PageInfo<PayableAccrued> getAllPayableAccrued(Integer contractId, Integer accruedId) {
        Example example = new Example(PayableAccrued.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        if (contractId != null && contractId >= 0){
            criteria.andEqualTo("contractId", contractId);
        }
        if (accruedId != null && accruedId >= 0){
            criteria.andEqualTo("id", accruedId);
        }
        List<PayableAccrued> payableAccruedList = this.payableAccruedMapper.selectByExample(example);
        for (PayableAccrued payableAccrued : payableAccruedList) {
            payableAccrued.setBillNumber(this.payableBillServiceImpl.getBillNumber(payableAccrued));
            payableAccrued.setAccruedOperationStageName(AccruedOperationStageEnum.int2Enum(payableAccrued.getAccruedOperationStageValue()).getName());
        }
        return new PageInfo<>(payableAccruedList);
    }

    @Override
    public Integer createOnePayableAccrued(BigDecimal settlementAmount) {
        PayableAccrued payableAccrued = new PayableAccrued(settlementAmount);
        int i = this.payableAccruedMapper.insert(payableAccrued);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.创建失败.getValue();
    }

    @Override
    public Integer compileOnePayableAccrued(PayableAccrued payableAccrued) {
        int i = this.payableAccruedMapper.updateByPrimaryKey(payableAccrued);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.更新失败.getValue();
    }

    @Override
    public Integer deleteOnePayableAccrued(Integer id) {
        PayableAccrued payableAccrued = this.getPayableAccrued(id);
        payableAccrued.setEnableStatusValue(EnableStatusEnum.弃用.getValue());
        int i = this.payableAccruedMapper.updateByPrimaryKey(payableAccrued);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.删除失败.getValue();
    }

    @Override
    public PayableAccrued getPayableAccrued(Integer id) {
        return this.payableAccruedMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<Integer, BigDecimal> getMapPayableAccrued() {
        Example example = new Example(PayableAccrued.class);
        example.createCriteria().andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue())
                .andLessThanOrEqualTo("accruedOperationStageValue", AccruedOperationStageEnum.已审核.getValue());
        List<PayableAccrued> payableAccruedList = this.payableAccruedMapper.selectByExample(example);
        Map<Integer,BigDecimal> result = new HashMap<>();
        for (PayableAccrued payableAccrued : payableAccruedList) {
            if (payableAccrued.getContractId() != null){
                if (result.get(payableAccrued.getContractId()) == null){
                    result.put(payableAccrued.getContractId(), BigDecimalUtil.ZERO);
                }
                result.put(payableAccrued.getContractId(), result.get(payableAccrued.getContractId()).add(payableAccrued.getSettlementAmount()));
            }
        }
        return result;
    }
}
